home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15008 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.3 KB

  1. Path: news.umbc.edu!not-for-mail
  2. From: schlein@umbc.edu (Jonas J. Schlein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: How to use assert( )
  5. Date: 16 Apr 1996 14:16:03 -0400
  6. Organization: University of Maryland Baltimore County
  7. Message-ID: <4l0o53$74s@umbc9.umbc.edu>
  8. References: <4kc3k7$dur@orion.cybercom.net>
  9. NNTP-Posting-Host: umbc9.umbc.edu
  10. NNTP-Posting-User: schlein
  11.  
  12. John Nield <nield@cybercom.net> wrote:
  13. |> I am interested in the use of the assert() library function (macro?).
  14. |> Neither the FAQ, nor K&R seems to have anything about it. Could
  15. |> someone help me with a breif explanation of its uses and mabye some
  16. |> example code? 
  17. |> 
  18. |> I'm just starting my first project big enough to split among many
  19. |> people, and from the vague explanations I've heard, assert is supposed
  20. |> to be a usefull way to cause errors when someone passes your code bad
  21. |> values. How do I do this?
  22.  
  23. Simply do an #include <assert.h> at the top of your code and then use
  24. assert (expr); wherever you want to test. Obviously expr is some boolean
  25. or logical expression. Should the assertion fail your program will and and
  26. print the expression it was passed as well as the line number and file.
  27.  
  28. Not the best way to catch errors, but it's ok for cases you consider
  29. "impossible" to occur ;-).
  30. -- 
  31. "If it wasn't for C, we would be using BASI, PASAL, and OBOL."
  32.  
  33. Jonas J. Schlein  (schlein@gl.umbc.edu)
  34.